// 页面处理的函数集
import TABBAR from "@/common/enumerate/tabbar";
// 是否处在tababr页面
export function hasTabbar(path) {
	const url = path || getRoutePath(1);
	return TABBAR.includes(url);
}

// 页面跳转兼容跳转tabbar  
// 窗口动画 https://uniapp.dcloud.net.cn/api/router.html#animation
export function to(_page, animationType = "pop-in") {
	_page = _page.startsWith('pages/') ? _page : 'pages/' + _page
	if (hasTabbar(_page)) {
		uni.switchTab({
			url: '/' + _page,
		})
		return
	} else {
		uni.navigateTo({
			url: '/' + _page,
			// #ifdef APP-PLUS
			animationType,
			animationDuration: 300,
			// #endif
			fail: err => {
				console.log(err, "跳转失败");
			}
		})
		return
	}
}


export function toLogin(isClose = false) {
	let path = 'public/login'
	isClose ? toRedi(path) : to(path);
}


// 关闭当前页跳转
export function toRedi(_page) {
	_page = _page.startsWith('pages/') ? _page : 'pages/' + _page
	if (hasTabbar(_page)) {
		uni.switchTab({
			url: '/' + _page,
		})
		return
	}
	uni.redirectTo({
		url: '/' + _page,
		fail: err => {
			console.log(err);
		}
	})
}

// 关闭所有页面跳转
export function toRela(_page) {
	_page = _page.startsWith('pages/') ? _page : 'pages/' + _page
	uni.reLaunch({
		url: '/' + _page,
	})
}

// tabbar跳转
export function toTab(_page) {
	_page = _page.startsWith('pages/') ? _page : 'pages/' + _page
	uni.switchTab({
		url: '/' + _page
	})
}

// 页面返回
export function toBack(delta = 1, animationType = 'pop-out') {
	uni.navigateBack({
		delta,
		// #ifdef APP-PLUS
		animationType,
		//#endif
	});
}

// 获取当前页面路径
export function getRoutePath(isFull = false) {
	let routers = getCurrentPages();
	let page = routers[routers.length - 1];
	let path;
	if (page) {
		path = isFull ? page.$page.fullPath.substring(1) : page.route;
	}
	return path
}